package ruifeng.zcj.dig.base.api;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import ruifeng.zcj.dig.groble.ConnUtils;
import ruifeng.zcj.mybase.base.utils2.RandomUtil;
import ruifeng.zcj.mybase.base.utils2.TimeUtil;

/**
 * 网络请求业务层的封装业务
 * Created by Administrator on 2016/9/18 0018.
 */
public class OSChinaApi2 {
    /**
     * 登陆
     *
     * @param username
     * @param password
     * @param handler
     */
    public static void login2(String username, String password,
                              StringCallback handler) {

        Map<String, String> params = getHeadParams2();

        params.put("username", username);
        params.put("password", password);

        OkHttpUtils
                .post()
                .url("http://kw.runfkj.com/appservice/login.action")
                .params(params)
                .build()
                .execute(handler);

    }

    public static HashMap getHeadParams2() {
        HashMap params = new HashMap();
        String timestamp = TimeUtil.getNowYMDHMSTime();
        String nonce = RandomUtil.create_nonce_str();
        params.put("signature", ConnUtils.getSignature(ConnUtils.token, timestamp, nonce));
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        return params;
    }

    /**
     * 发帖
     **/
    public static void sendPostings(String postingsname,
                                    String postingscontent,
                                    String firstsection,
                                    String cnfirstsection,
                                    String secondsection,
                                    String cnsecondsection,
                                    String fuserid,
                                    String istop,
                                    String city, File files, StringCallback handler) {
        Map<String, String> params = getHeadParams2();
        params.put("postingsname",postingsname);
        params.put("postingscontent",postingscontent);
        params.put("firstsection",firstsection);
        params.put("cnfirstsection",cnfirstsection);
        params.put("secondsection",secondsection);
        params.put("cnsecondsection",cnsecondsection);
        params.put("fuserid",fuserid);
        params.put("istop",istop);
        params.put("city",city);


     /*   Request request = new Request.Builder().url(url)
                .post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;charset=gb2312"),内容))
                .build();*/

        OkHttpUtils
                .post()
                .url("http://kw.runfkj.com/appservice/sendpostings.action")
                .addFile("1",files.getName(),files)
                .params(params)
                .build()
                .execute(handler);
       /* for (int i = 0; i < files.size(); i++) {
            params.put("1" + i, String.valueOf(files.get(i)));
        }*/

    }


}
